//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PathwiseVegasAccountingEngine.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/Evolvers/LogNormalFwdRateEuler.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels::Evolvers;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathwiseVegasAccountingEngine = NULL;
#endif
	_ppPathwiseVegasAccountingEngine = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (QuantLib::PathwiseVegasAccountingEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPathwiseVegasAccountingEngine = NULL;
#endif
	_ppPathwiseVegasAccountingEngine = new boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine> (&childNative);
    
    _PathwiseVegasAccountingEngineOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (CPathwiseVegasAccountingEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathwiseVegasAccountingEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPathwiseVegasAccountingEngine = new boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathwiseVegasAccountingEngine = NULL;
#endif
	if (!t->IsSubclassOf(CPathwiseVegasAccountingEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (QuantLib::Handle<QuantLib::PathwiseVegasAccountingEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPathwiseVegasAccountingEngine = &childNative;
	_ppPathwiseVegasAccountingEngine = &static_cast<boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine>>(childNative.currentLink());
    
    _PathwiseVegasAccountingEngineOwner = owner;
}
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (QuantLib::Handle<QuantLib::PathwiseVegasAccountingEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPathwiseVegasAccountingEngine = &childNative;
	_ppPathwiseVegasAccountingEngine = &static_cast<boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::CPathwiseVegasAccountingEngine (QuantLib::PathwiseVegasAccountingEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathwiseVegasAccountingEngine = NULL;
#endif
	_ppPathwiseVegasAccountingEngine = new boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine> (new QuantLib::PathwiseVegasAccountingEngine (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::~CPathwiseVegasAccountingEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPathwiseVegasAccountingEngine != NULL)
    {
	    delete _ppPathwiseVegasAccountingEngine;
        _ppPathwiseVegasAccountingEngine = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::!CPathwiseVegasAccountingEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPathwiseVegasAccountingEngine != NULL)
    {
	    delete _ppPathwiseVegasAccountingEngine;
    }
}
QuantLib::PathwiseVegasAccountingEngine& Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::GetReference ()
{
    if (_ppPathwiseVegasAccountingEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppPathwiseVegasAccountingEngine;
}
boost::shared_ptr<QuantLib::PathwiseVegasAccountingEngine>& Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::GetShared ()
{
    if (_ppPathwiseVegasAccountingEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppPathwiseVegasAccountingEngine;
}
QuantLib::PathwiseVegasAccountingEngine* Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::GetPointer ()
{
    if (_ppPathwiseVegasAccountingEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppPathwiseVegasAccountingEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PathwiseVegasAccountingEngine>& Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::GetHandle ()
{
	if (_phPathwiseVegasAccountingEngine == NULL)
	{
		_phPathwiseVegasAccountingEngine = new Handle<QuantLib::PathwiseVegasAccountingEngine> (*_ppPathwiseVegasAccountingEngine);
	}
	return *_phPathwiseVegasAccountingEngine;
}
#endif
bool Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::HasNative () 
{
	return (_ppPathwiseVegasAccountingEngine != NULL);
}

Cephei::QL::Models::Marketmodels::IPathwiseVegasAccountingEngine^ Cephei::QL::Models::Marketmodels::CPathwiseVegasAccountingEngine::MultiplePathValues (Cephei::Core::IVector<Double>^ means, Cephei::Core::IVector<Double>^ errors, UInt64 numberOfPaths)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        means->Lock();
        INativeVector<Double>^ _NCImeans = means->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCmeans = safe_cast<CDoubleVector^>(_NCImeans);
        std::vector<QuantLib::Real>& _means = static_cast<std::vector<QuantLib::Real>&> (_NCmeans->GetReference ());
        errors->Lock();
        INativeVector<Double>^ _NCIerrors = errors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCerrors = safe_cast<CDoubleVector^>(_NCIerrors);
        std::vector<QuantLib::Real>& _errors = static_cast<std::vector<QuantLib::Real>&> (_NCerrors->GetReference ());
        QuantLib::Size _numberOfPaths = (QuantLib::Size)ValueHelper::Convert (numberOfPaths); //a
    	(*_ppPathwiseVegasAccountingEngine)->multiplePathValues ( _means,  _errors,  _numberOfPaths );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (means != nullptr) means->Unlock(); //NO2  
        if (errors != nullptr) errors->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

